/**
 * Copyright (c) qzsoft All rights reserved.
 *
 * qzsoft.cn
 *
 * 版权所有，侵权必究！
 */

package com.qzsoft.system.modules.sys.noticeTemplate.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzsoft.jeemis.common.constant.Constant;
import com.qzsoft.jeemis.common.service.BaseService;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.system.common.CommonUtils;
import com.qzsoft.system.modules.sys.notice.dao.SysNoticeFileDao;
import com.qzsoft.system.modules.sys.notice.entity.SysNoticeFileEntity;
import com.qzsoft.system.modules.sys.noticeTemplate.dao.SysNoticeTemplateDao;
import com.qzsoft.system.modules.sys.noticeTemplate.entity.SysNoticeTemplateEntity;
import com.qzsoft.system.modules.sys.noticeTemplate.service.SysNoticeTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 * 
 * @author
 */
@Service
public class SysNoticeTemplateServiceImpl extends BaseService implements SysNoticeTemplateService {

	@Autowired
	private SysNoticeTemplateDao noticeTemplateDao;
	@Autowired
	private SysNoticeFileDao sysNoticeFileDao;
	@Override
	public IPage<SysNoticeTemplateEntity> page(Page page, SysNoticeTemplateEntity params) {
		String title = params.getTitle();
		if (StringUtils.isNotBlank(title)){
			if (CommonUtils.check(title)){
				title = CommonUtils.replaceSqlLikeField(title);
				params.setTitle(title);
			}
		}
		if(StringUtils.isNotBlank(params.getOrder())){
			if(params.getOrder().startsWith(Constant.ASC)){
				params.setOrder(Constant.ASC);
			} else {
				params.setOrder(Constant.DESC);
			}
		}
		IPage<SysNoticeTemplateEntity> pageData  = noticeTemplateDao.findList(page, params);
		pageData= ConvertDictUtils.formatDicDataPage(pageData);
		return pageData;
	}

	@Override
	public List<SysNoticeTemplateEntity> list(Map<String, Object> params) {
		QueryWrapper<SysNoticeTemplateEntity> queryWrapper = this.getQueryWrapper(params,SysNoticeTemplateEntity.class,"title");;
		List<SysNoticeTemplateEntity> entityList = noticeTemplateDao.selectList(queryWrapper);
		entityList=ConvertDictUtils.formatDicDataList(entityList);
		return entityList;
	}

	@Override
	public SysNoticeTemplateEntity get(String id) {
		SysNoticeTemplateEntity entity = noticeTemplateDao.selectById(id);
		QueryWrapper<SysNoticeFileEntity> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("notice_id",id).orderByAsc("orderid","create_date");
		List<SysNoticeFileEntity> fileEntityList=sysNoticeFileDao.selectList(queryWrapper);
		entity=ConvertDictUtils.formatDicData(entity);
		entity.setFileList(fileEntityList);
		return entity;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(SysNoticeTemplateEntity entity) {
		entity.setId(IdWorker.get32UUID());
		entity.setDeptId(this.basegetLoginUser().getDeptId());
//		if (entity.getOrderid()==null){
//			entity.setOrderid(1);
//		}
//		entity.setDeptCode(this.basegetLoginUser().getDeptCode());
		noticeTemplateDao.insert(entity);
		// 保存附件
		this.saveNoticeFiles(entity);
	}

	/**
	 * 保存附件
	 * @param entity
	 */
	private  void saveNoticeFiles(SysNoticeTemplateEntity entity){
		QueryWrapper<SysNoticeFileEntity> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("notice_id",entity.getId());
		sysNoticeFileDao.delete(queryWrapper);
		List<SysNoticeFileEntity> fileEntityList=entity.getFileList();
		int i=1;
		for (SysNoticeFileEntity fileEntity :  fileEntityList) {
			fileEntity.setId(IdWorker.get32UUID());
			fileEntity.setNoticeId(entity.getId());
			fileEntity.setOrderid(i++);
			fileEntity.setFileType(FileUtil.extName(fileEntity.getFileName()).toLowerCase());
			sysNoticeFileDao.insert(fileEntity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysNoticeTemplateEntity entity) {
		noticeTemplateDao.updateById(entity);
		// 保存附件
		this.saveNoticeFiles(entity);
	}

	@Override
	public void updateStatus(SysNoticeTemplateEntity entity) {
		UpdateWrapper<SysNoticeTemplateEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", entity.getId());
		noticeTemplateDao.update(entity, updateWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(String[] ids) {
		//删除通知模板
		noticeTemplateDao.deleteBatchIds(Arrays.asList(ids));
		// 删除附件
		QueryWrapper<SysNoticeFileEntity> queryWrapper=new QueryWrapper<>();
		queryWrapper.in("notice_id",ids);
		sysNoticeFileDao.delete(queryWrapper);
	}
}
